package preprocess;

import java.util.*;

import FlowGraph.*;
import SymbolTable.*;
import FlowGraph.Nodes.*;
import util.Util;
import util.InfoCollector;
import db.ClassData;

//////////////////////////////////////////////////////////////////////////
//// SDFInfoCollector
/**
@author Ivan Jeukens
@version v 0.1 18/10/2002
*/
public class SDFInfoCollector implements InfoCollector {

    public SDFInfoCollector() {
        _sdfPorts = new HashMap();
        _sdfMethod = new LinkedList();
    }
    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void visitFGNode(FlowGraphNode node) {
        if(node.getType() != FlowGraphNode.METHOD_CALL) return;

        MethodCallNode mc = (MethodCallNode) node;

        String mname = mc.getMethodName();
        Class c = mc.getCallerType();
        if(mname != null && c != null && _isSDFMethod(mname) &&
               Util.isSubClass(c, ptolemy.domains.sdf.kernel.SDFIOPort.class)) {
            _sdfMethod.add(node);
        }
    }

    // O Owner aqui vai ser sempre null
    public void visitSTEntry(STEntry entry, FlowGraphNode owner, ClassData info) {
        if(entry instanceof STPortEntry) {
            Class type = entry.getType();
            if(type.equals(ptolemy.domains.sdf.kernel.SDFIOPort.class)) {
                _sdfPorts.put(entry, info);
            }
        }
    }

    public Iterator sdfMethodIterator() {
        return _sdfMethod.iterator();
    }

    public int sdfMethodSize() {
        return _sdfMethod.size();
    }

    public int sdfPortsSize() {
        return _sdfPorts.size();
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private methods                   ////

    private boolean _isSDFMethod(String name) {
        if(name.equals("getTokenConsumptionRate") ||
           name.equals("getTokenProductionRate") ||
           name.equals("getTokenInitProduction") ||
           name.equals("setTokenConsumptionRate") ||
           name.equals("setTokenProductionRate") ||
           name.equals("setTokenInitProduction")) return true;
           
        return false;
    }
   

    ///////////////////////////////////////////////////////////////////
    ////                       private variables                   ////

    private HashMap _sdfPorts;
    private LinkedList _sdfMethod;
}
